//
//  addressListsViewController.m
//  giuseppe
//
//  Created by Apple on 15/8/18.
//  Copyright (c) 2015年 GiantUmbrella. All rights reserved.
//

#import "addressListsViewController.h"
#import "define.h"
#import "addressEditViewController.h"

@interface addressListsViewController ()<UITableViewDataSource,UITableViewDelegate>{
    UITableView *_tableView;
    BOOL edit;
    UILabel *titleLabel;
    UIButton *editButton;
}

@end

@implementation addressListsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController setToolbarHidden:YES animated:YES];
    
    edit = NO;
    
    //自定义的导航栏
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    navView.backgroundColor = [UIColor whiteColor];
    navView.alpha = 0.90;
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(10, 33, 27, 16);
    [backButton setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth/2-75, 20, 150, 40)];
    titleLabel.text = @"选择收货地址";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    editButton.frame = CGRectMake(kScreenWidth-10-50, 33, 50, 16);
    [editButton setTitle:@"编辑" forState:UIControlStateNormal];
    [editButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [editButton addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];

    [self _initView];
    
    [navView addSubview:backButton];
    [navView addSubview:titleLabel];
    [navView addSubview:editButton];
    [self.view addSubview:navView];

}


- (void)_initView{
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake( 0, 64, kScreenWidth, kScreenHeight-64)];
    _tableView.delegate = self;
    _tableView .dataSource = self;
    
    [self.view addSubview:_tableView];
    
    
}


#pragma mark--- tableViewDelegate 每个区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 4;
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 90;
}


#pragma mark--- tableViewDelegate cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
 
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        
        UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, kScreenWidth-15, 10)];
        
        UILabel *mobileLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 30, kScreenWidth-15, 10)];
        
        UILabel *addressLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 50, kScreenWidth-15, 10)];
        
        UILabel *postCodeLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 70, kScreenWidth-15, 10)];
        
        nameLabel.font = [UIFont systemFontOfSize:13];
        mobileLabel.font = [UIFont systemFontOfSize:13];
        addressLabel.font = [UIFont systemFontOfSize:13];
        postCodeLabel.font = [UIFont systemFontOfSize:13];
        
        nameLabel.text = @"小明";
        mobileLabel.text = @"13023660332";
        addressLabel.text = @"浙江省杭州市西湖区文新街道古墩路98号西城新座16D";
        postCodeLabel.text = @"310013";
        
        [cell.contentView addSubview:nameLabel];
        [cell.contentView addSubview:mobileLabel];
        [cell.contentView addSubview:addressLabel];
        [cell.contentView addSubview:postCodeLabel];
    
    
    if (edit) {
         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    
    return cell;
    
    
}


#pragma mark--- tableViewDelegat 点击方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (edit) {
        
        addressEditViewController *addressEditVC = [[addressEditViewController alloc]init];
        [self.navigationController pushViewController:addressEditVC animated:YES];
        
        
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
    

    

    
}

- (void)edit{
    
    if (edit) {
        
        edit = NO;
        titleLabel.text = @"选择收货地址";
        [editButton setTitle:@"编辑" forState:UIControlStateNormal];
        [_tableView reloadData];

    }else{
        
        edit = YES;
        titleLabel.text = @"编辑收货地址";
        [editButton setTitle:@"完成" forState:UIControlStateNormal];
        [_tableView reloadData];
    }
    
    
}



- (void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
